import java.awt.geom.Line2D;

public class Phaser
{
    double slope;
    double velocityX, velocityY;
    double width, height;

    Line2D.Double line;

    /*
     * Creates a new phaser object that will shoot from one point to another
     * at a certain speed.
     */
    public Phaser (int initialX, int initialY, int endX, int endY, int offset, int length, int speed)
    {
      try
      {
        slope = (double) (endY - initialY) / (endX - initialX);
      }
      catch (Exception e)
      {
        slope = Double.MAX_VALUE;
      }
      
      double angle = Math.atan (slope);
      velocityX = Math.cos (angle) * speed;
      velocityY = Math.sin (angle) * speed;
      width = Math.cos (angle) * length;
      height = Math.sin (angle) * length;
      
      if (endX < initialX)
      {
        velocityX = -velocityX;
        width = -width;
      }
      if ((endY < initialY && angle > 0) || (endY > initialY && angle < 0))
      {
        velocityY = -velocityY;
        height = -height;
        
      }
      
      
      line = new Line2D.Double ((double) initialX, (double) initialY, initialX + width, initialY + height);
      
      // Gets the bullet away from the starting position, so the bullet won't hit the shooter
      while (Math.abs(line.getX1() - initialX) < offset && Math.abs(line.getY1() - initialY) < offset)
        update();
    }
    
    
    /*
     * Updates this phaser to continue moving
     */
    public void update ()
    {
      line.setLine (line.getX1 () + velocityX, line.getY1 () + velocityY, line.getX2 () + velocityX, line.getY2 () + velocityY);
    }
}
